package a;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet("/ServletRequestDemo1")
public class ServletRequestDemo1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取请求方式
        String method = request.getMethod();
        System.out.println(method);
        //2.获取URI -> 虚拟目录 + 具体路径
        String uri = request.getRequestURI();
        System.out.println("uri:"+uri);
        StringBuffer url1 = request.getRequestURL();
        String url = url1.toString();
        System.out.println("url:"+url);
        //3.获取协议版本
        String protocol = request.getProtocol();
        System.out.println("protocol:"+protocol);
        //4.获取虚拟目录
        String context = request.getContextPath();
        System.out.println("contextPath虚拟目录:"+context);
        //5.获取Servlet路径
        String servlet = request.getServletPath();
        System.out.println("servletPath资源路径:"+servlet);
        //6.获取请求参数 - 只适用于get的提交方式
        String query = request.getQueryString();
        System.out.println("query:"+query);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.getWriter().write(new Date().toString());
    }
}
